﻿var bigPXoffset;
var bigPYoffset;
var t1, t2;
jQuery(document).ready(function () {
    setcarousel();
    setJqzoom();
    setSize();
});
function setSelProductbackground() {
    $(".selProduct").mouseenter(selProductEnter);
    $(".selProduct").mousemove(selProductEnter).mouseout(selProductOut);
}
function setcarousel() {
    if ((t1 != null) || (t1 != undefined)) clearInterval(t1);
    if ((t2 != null) || (t2 != undefined)) clearInterval(t2);
    jQuery('#mycarousel').jcarousel({ initCallback: mycarousel_initCallback });
    setShoppingUrl();
}
function mycarousel_initCallback(carousel) {
    var index = 0;
    $("#mycarousel li img").eq(index).attr("class", "curr");
    $("#mycarousel li").click(function () {
        index = $("#mycarousel li").index(this);
        $("#productImage")[0].src = this.getElementsByTagName("img")[0].src.replace("small", "mid");
        changeViewImg();
        $(this).siblings().each(function () {
            this.getElementsByTagName("img")[0].className = "";
        })
        this.getElementsByTagName("img")[0].className = "curr";

    });

    $("#mycarousel li").hover(function () {
        $("#productImage")[0].src = this.getElementsByTagName("img")[0].src.replace("small", "mid");
        this.getElementsByTagName("img")[0].className = "curr";

    },
				function () {
				    var currIndex = $("#mycarousel li").index(this);
				    if (!(index == currIndex)) {
				        this.getElementsByTagName("img")[0].className = "";
				    }
				    $("#productImage")[0].src = $("#mycarousel li img").eq(index).attr("src").replace("small", "mid");
				});


    temp_carousel = carousel;
    carousel.buttonNext.hover(function () {
        t1 = setInterval("temp_carousel.buttonNext.click()", 500);
        if (typeof t2 != "undefined") clearInterval(t2);
    },
				 function () {
				     clearInterval(t1);
				 });
    carousel.buttonPrev.hover(function () {
        t2 = setInterval("temp_carousel.buttonPrev.click()", 500);
        if (typeof t1 != "undefined") clearInterval(t1);
    }, function () {
        clearInterval(t2);
    });
}



function mouseEnter(e) {
    if ($("#winSelector").css("display") == "none") {
        $("#winSelector,#bigView").show();
    }

    $("#winSelector").css(fixedPosition(e));
    e.stopPropagation();
}

//    function mouseMove(e) {
//        $("#winSelector").css(fixedPosition(e));
//        e.stopPropagation();
//    }

function mouseOut(e) {
    if ($("#winSelector").css("display") != "none") {
        $("#winSelector,#bigView").hide();
    }
    e.stopPropagation();
}




var $divWidth = $("#winSelector").width(); //选择器宽度
var $divHeight = $("#winSelector").height(); //选择器高度
var $imgWidth = $("#productImage").width(); //中图宽度
var $imgHeight = $("#productImage").height(); //中图高度
var $viewImgWidth, $viewImgHeight, $height; //IE加载后才能得到 大图宽度 大图高度 大图视窗高度

function changeViewImg() {
    $("#bigView img").attr("src", $("#productImage").attr("src").replace("mid", "big"));
}

function fixedPosition(e) {
    if (e == null) {
        return;
    }
    var $imgLeft = $("#productImage").offset().left; //中图左边距
    var $imgTop = $("#productImage").offset().top; //中图上边距
    X = e.pageX - $imgLeft - $divWidth / 2; //selector顶点坐标 X
    Y = e.pageY - $imgTop - $divHeight / 2; //selector顶点坐标 Y
    X = X < 0 ? 0 : X;
    Y = Y < 0 ? 0 : Y;
    X = X + $divWidth > $imgWidth ? $imgWidth - $divWidth : X;
    Y = Y + $divHeight > $imgHeight ? $imgHeight - $divHeight : Y;

    if ($viewImgWidth == null) {
        $viewImgWidth = $("#bigView img").outerWidth();
        $viewImgHeight = $("#bigView img").height();
        if ($viewImgWidth < 150 || $viewImgHeight < 150) {
            $viewImgWidth = $viewImgHeight = 800;
        }
        $height = $divHeight * $viewImgHeight / $imgHeight;
        $("#bigView").width($divWidth * $viewImgWidth / $imgWidth);
        $("#bigView").height($height);
    }

    var scrollX = X * $viewImgWidth / $imgWidth;
    var scrollY = Y * $viewImgHeight / $imgHeight;
    $("#bigView img").css({ "left": scrollX * -1, "top": scrollY * -1 });


    //确定上边距
    //用户视窗高度
    //var viewH = document.documentElement.clientHeight == 0 ? document.body.clientHeight : document.documentElement.clientHeight;
    //var top = ((viewH - $height) / 2) + $(document).scrollTop();
    //top = top < 150 ? 150 : top;
    var left = $(".singleAreaRight")[0].offsetLeft; //固定位置
    var top = $(".singleAreaRight")[0].offsetTop + 24;
    if ($(window).width() > $(document.body).width()) {
        //left = left - (($(window).width() - $(document.body).width()) / 2);
    }
    $("#bigView").css({ "top": top, "left": left });


    return { left: X, top: Y };
}

function setJqzoom() {
    $viewImgWidth = $viewImgHeight = $height = null; //IE加载后才能得到 大图宽度 大图高度 大图视窗高度
    $("#productImage").mouseenter(mouseEnter); //中图事件
    $("#productImage,#winSelector").mousemove(mouseEnter).mouseout(mouseOut); //选择器事件
    changeViewImg();
    $("#bigView").scrollLeft(0).scrollTop(0);
}
function setSize() {
    if ($("#All_Size li").length == 1) {
        $("#All_Size li").eq(0).click();
    }
}

function showShareDiv(id, title, imgurl, price, link) {
    if (imgurl == "") {
        alert("该商品暂无图片，无法分享")
    } else {
        truePrice = "";
        var truePriceD = $("span[class='cssPrice']");
        if (truePriceD.length > 1) {
            truePrice = truePriceD.eq(0).html() + "(加" + truePriceD.eq(1).html() + "积分)";
        } else {
            truePrice = truePriceD.eq(0).html();
        }

        if (truePrice != null && truePrice != "") {
            price = truePrice;
        }

        windowWidth = "";
        windowHeight = "";

        if (navigator.userAgent.indexOf("MSIE") > 0) { //是否是IE浏览器 
            if (navigator.userAgent.indexOf("MSIE 6.0") > 0) { //6.0 使用1.CSS 

                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight + 90
            }
            else if (navigator.userAgent.indexOf("MSIE 7.0") > 0) {//7.0 使用2.CSS 

                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight
            } else {
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            }

        } else {//否则使用3.CSS 还有具体的浏览器 你可以用navigator.userAgent 去获取信息 具体使用 看你自己 

            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        }

        var popupHeight = $("#shareDiv").height();
        var popupWidth = $("#shareDiv").width();
        $("#shareDiv").css({
            "position": "absolute",
            "top": windowHeight / 2 - popupHeight / 2 + 130,
            "left": windowWidth / 2 - popupWidth / 2 + 55
        });
        if (link.indexOf("?") != -1) {
            $("#shareLink").val(link + "&source=vanclshare")
        } else {
            $("#shareLink").val(link + "?source=vanclshare")
        }

        $("#shareDiv").show()
        $("#kaixinLink").attr("href", "/cooperate/kaixin.ashx?iid=" + escape(id) + "&title=" + escape(title) + "&pic=" + escape(imgurl) + "&price=" + escape(price) + "&link=" + escape(link))
    }
}

function copyToClipboard(txt) {
    if (window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
        alert("复制成功！")
    } else if (navigator.userAgent.indexOf("Opera") != -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'");
        }
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip)
            return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans)
            return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext = txt;
        str.data = copytext;
        trans.setTransferData("text/unicode", str, copytext.length * 2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip)
            return false;
        clip.setData(trans, null, clipid.kGlobalClipboard);
        alert("复制成功！")
    }
}
//js-yq-start
function setTab(name, cursel, n) {
    if (cursel != 1) {
        $(".TagTitle").hide();
    }
    else {
        $(".TagTitle").show();
    }
    for (i = 1; i <= n; i++) {
        var menu = document.getElementById(name + i);
        var con = document.getElementById("con_" + name + "_" + i);
        if (cursel == 1) {
            con.style.display = "block";
        }
        else {
            con.style.display = i == cursel ? "block" : "none";
        }
        menu.className = i == cursel ? "hover" : "";
    }
}

var StyleID = 0, CommentOrderType = 0, QuestionOrderType = 0; //定义款式编号，排序方式变量

$("#hdnQuestionsListIsLoad").attr("value", "0");
$("#hdnCommentListIsLoad").attr("value", "0");
$("#hdnPayType").attr("value", "0");
$("#hdnAfterServices").attr("value", "0");
$("#hdnHowBuy").attr("value", "0");
function GetCommentList() {
    $.ajax({
        type: "GET",
        url: "/Styles/GetCommentList.mvc?pageindex=1&styleid=" + StyleID + "&ordertype=" + CommentOrderType + "&pagesize=5&rad=" + Math.random(),
        dataType: "text",
        success: function (msg) {
            $("#CommentList").html(msg);
            $("#hdnCommentListIsLoad").attr("value", "1");
        }
    })
}
function GetQuestionList() {
    $.ajax({
        type: "GET",
        url: "/Styles/GetQuestionsList.mvc?pageindex=1&styleid=" + StyleID + "&ordertype=" + QuestionOrderType + "&pagesize=5&rad=" + Math.random(),
        dataType: "text",
        success: function (msg) {
            $("#QuestionsList").html(msg);
            $("#hdnQuestionsListIsLoad").attr("value", "1");
        }
    })
}
function GetPayType() {
    if ($("#hdnPayType").attr("value") == "0") {
        $.ajax({
            type: "GET",
            url: getHost() + "/Templates/DetailStatic/PayType.html",
            dataType: "text",
            success: function (msg) {
                $("#DivPayType").html(msg);
                $("#hdnPayType").attr("value", "1")
            }
        })
    }
}
function GetAfterServices() {
    if ($("#hdnAfterServices").attr("value") == "0") {
        $.ajax({
            type: "GET",
            url: getHost() + "/Templates/DetailStatic/AfterServices.html",
            dataType: "text",
            success: function (msg) {
                $("#DivAfterServices").html(msg);
                $("#hdnAfterServices").attr("value", "1");
            }
        })
    }
}
function GetHowBuy() {
    if ($("#hdnHowBuy").attr("value") == "0") {
        $.ajax({
            type: "GET",
            url: getHost() + "/Templates/DetailStatic/HowBuy.html",
            dataType: "text",
            success: function (msg) {
                $("#DivHowBuy").html(msg);
                $("#hdnHowBuy").attr("value", "1");
            }
        })
    }
}
function ScrollToComment() {
    var scrolltop = $("#Comment").offset().top;
    window.scrollTo(0, scrolltop);
}
function ScrollToQuestions() {
    var scrolltop = $("#Questions").offset().top;
    window.scrollTo(0, scrolltop);
}
function ScrollSizeDescription() {
    var scrolltop = $("#LikeSizeDescription").offset().top;
    window.scrollTo(0, scrolltop);
}
function ChangeCommentOrderType(type) {//切换排序方式
    CommentOrderType = type;
    $("#hdnCommentListIsLoad").attr("value", "0");
    CommentList();
}
function ChangeQuestionOrderType(type) {//切换排序方式
    QuestionOrderType = type;
    $("#hdnQuestionsListIsLoad").attr("value", "0");
    QuestionList();
}
function CommentList() {//加载评论
    if ($("#hdnCommentListIsLoad").attr("value") == "0") {
        GetCommentList();
    }
}
function QuestionList() {//加载问答
    if ($("#hdnQuestionsListIsLoad").attr("value") == "0") {
        GetQuestionList();
    }
}
function getHost(url) {
    var host = "null";
    if (typeof url == "undefined"
                            || null == url)
        url = window.location.href;
    var regex = /(.*\:\/\/[^\/]*).*/;
    var match = url.match(regex);
    if (typeof match != "undefined"
                            && null != match)
        host = match[1];
    return host;
}
function scrollAjax() {
    try {
        window.onscroll = wheel;
    }
    catch (ex) { }
}
function wheel()//页面滚动到指定坐标刷新相对应部分
{
    var commentTop = $("#Comment").offset().top;
    var questionsTop = $("#Questions").offset().top;
    var payTpyeTop = $("#con_tabs_4").offset().top;
    var afterServicesTop = $("#con_tabs_5").offset().top;
    var howBuyTop = $("#con_tabs_6").offset().top;
    var nowtop = document.documentElement.scrollTop;
    if (nowtop == 0)//兼容Chrome浏览器
    {
        nowtop = document.body.scrollTop;
    }
    if ((nowtop + document.documentElement.clientHeight) > commentTop) {
        CommentList();
    }
    if ((nowtop + document.documentElement.clientHeight) > questionsTop) {
        QuestionList();
    }
    if ((nowtop + document.documentElement.clientHeight) > payTpyeTop) {
        GetPayType();
    }
    if ((nowtop + document.documentElement.clientHeight) > afterServicesTop) {
        GetAfterServices();
    }
    if ((nowtop + document.documentElement.clientHeight) > howBuyTop) {
        GetHowBuy();
    }
}
function ShowHideVip() {
    if ($(".vipWindows").css("display") == "none") {
        $(".vipWindows").show();
    }
    else {
        $(".vipWindows").hide();
    }
}
function selProductEnter(e) {
    $(".selProduct").css("background", "#FAE3E3");
}
function selProductOut(e) {
    $(".selProduct").css("background", "#FFF0F0");
}
function goToTop() {
    var viewH = document.documentElement.clientHeight == 0 ? document.body.clientHeight : document.documentElement.clientHeight;
    var Vwidth = $(".singleArea").width();
    var goup;
    if (window.XMLHttpRequest) {
        goup = $("<div style=\"right:-50px; POSITION: absolute;z-index:1000;\"><a href=\"#\"><img src=\"http://images.vjia.com/Product/SingleProduct/top.gif\" alt=\"top\" /></a></div>").hide();
        goup.css({ position: "fixed", top: viewH - 66, left: ($("body").width() - Vwidth) / 2 + Vwidth + 5 });
    } else {
        goup = $("<div style=\"POSITION: absolute;z-index:1000;\"><a href=\"#\"><img src=\"http://images.vjia.com/Product/SingleProduct/top.gif\" alt=\"top\" /></a></div>").hide();
        if (document.documentElement.clientWidth > Vwidth + 30) {
            if ($(window).scrollTop() > 0) {
                goup.css({ top: $(window).scrollTop() + viewH - 66, left: ($("body").width() - Vwidth) / 2 + Vwidth + 5 }).show();
            }
        }
    }
    $(window).scroll(function () {
        viewH = document.documentElement.clientHeight == 0 ? document.body.clientHeight : document.documentElement.clientHeight;
        if ($(window).scrollTop() == 0) {
            goup.fadeOut();
        } else {
            goup.fadeIn();
            if (!window.XMLHttpRequest) {
                if (document.documentElement.clientWidth <= Vwidth + 30) {
                    goup.hide();
                }
                else {
                    goup.css({ top: $(window).scrollTop() + viewH - 66, left: ($("body").width() - Vwidth) / 2 + Vwidth + 5 });
                }
            }
            else {
                goup.css({ position: "fixed", top: viewH - 66, left: ($("body").width() - Vwidth) / 2 + Vwidth + 5 });
            }
        }
    });
    $("body").prepend(goup);
}
